package com.example.weather;

import android.app.Activity;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity  {

    private ViewPager viewPager;
    private ViewPagerAdapter pagerAdapter;
    private TabLayout tabLayout;
    private List<Fragment> listfragment;
    private View view1,view2,view3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.vp_result);
        tabLayout = findViewById(R.id.tablayout);
        initPager();
    }

    private void initPager() {
        /*LayoutInflater inflater=getLayoutInflater();
        view1 = inflater.inflate(R.layout.layout1, null);
        view2 = inflater.inflate(R.layout.layout2,null);
        view3 = inflater.inflate(R.layout.layout3, null);

        viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);*/

        listfragment = new ArrayList<>();
        listfragment.add(new GzFragment());
        listfragment.add(new BjFragment());
        listfragment.add(new ShFragment());
        listfragment.add(new CxFragment());

        /*pagerAdapter = new ViewPagerAdapter(MainActivity.this,viewList,titleList);
        viewPager.setAdapter(pagerAdapter);*/
        pagerAdapter=new ViewPagerAdapter(getSupportFragmentManager(),listfragment,this);
        //ViewPager限定预加载的页面个数
        viewPager.setOffscreenPageLimit(4);
        viewPager.setAdapter(pagerAdapter);
//        //设置分割线
        tabLayout.setTabMode(TabLayout.GRAVITY_CENTER);
        tabLayout.setupWithViewPager(viewPager);
//        LinearLayout linear = (LinearLayout)tabLayout.getChildAt(0);
//        linear.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
//        linear.setDividerDrawable(ContextCompat.getDrawable(this,R.color.colorAccent));

    }


}
